import AES from 'crypto-js/aes';
import Utf8 from 'crypto-js/enc-utf8';
//模式 cbc 是默认模式不需要设置
import cfb from 'crypto-js/mode-cfb';
import ctr from 'crypto-js/mode-ctr';
import ctrgladman from 'crypto-js/mode-ctr-gladman';
import ofb from 'crypto-js/mode-ofb';
import ecb from 'crypto-js/mode-ecb';
//填充
import pkcs7 from 'crypto-js/pad-pkcs7'; 
import ansix923 from 'crypto-js/pad-ansix923';
import iso10126 from 'crypto-js/pad-iso10126';
import iso97971 from 'crypto-js/pad-iso97971';
import zeropadding from 'crypto-js/pad-zeropadding';
import nopadding from 'crypto-js/pad-nopadding';

/**
 * @note AES 算法单例对象
 * */
 let AESUtil = {}
 //模式
 let modeType = {cfb,ctr,ctrgladman,ofb,ecb}
 //填充
 let paddingType = {pkcs7,ansix923,iso10126,iso97971,zeropadding,nopadding}
 
/**
 * @note AES 加密
 * @param {String} data [ 需要加密的数据 ]
 * @param {String} key [ 秘钥 ]
 * @param {Object} opts [ 配置对象 ]
 * @return {String}
 * */
export function AESEncode(data,key,{
    //模式
    mode = 'cbc',
    //填充
    padding = 'pkcs7',
    // iv 偏移
    iv = ''
} = {}){
    let opts = {
        padding: paddingType[padding],
        iv:Utf8.parse(iv)
    };
    mode !== 'cbc' && (opts.mode = modeType[mode]);
    return AES.encrypt(Utf8.parse(data),Utf8.parse(key),opts).toString();
}
/**
 * @note AES 加密
 * @param {String} data [ 需要解密的数据 ]
 * @param {String} key [ 秘钥 ]
 * @param {Object} opts [ 配置对象 ]
 * @return {String}
 * */
export function AESDecode(data,key,{
    //模式
    mode = 'cbc',
    //填充
    padding = 'pkcs7',
    // iv 偏移
    iv = ''
} = {}){
    let opts = {
        padding: paddingType[padding],
        iv:Utf8.parse(iv)
    };
    mode !== 'cbc' && (opts.mode = modeType[mode]);
    return AES.decrypt(data,Utf8.parse(key),opts).toString(Utf8);
}
